LBITS := $(shell getconf LONG_BIT)
ifeq ($(LBITS),64)
   LD_PATH=externLibs/unix/64
else
   LD_PATH=externLibs/unix/32
endif

srcdir=src/
objdir=obj/
bindir=lib/
includedir=src/
includeLibs=externLibs/
CXX=g++
CXXFLAGS=-fPIC -Wall -DGLEW_BUILD -s -O2
LDFLAGS=-L$(LD_PATH) -lglfw -lGL -lGLU -lSOIL
EXE=libGlow.so
SRC= $(wildcard $(srcdir)*.cpp)
OBJ= $(SRC:%.cpp=$(objdir)%.o)

all: depend $(EXE)

$(EXE): $(OBJ)
	$(CXX) -o obj/externLibs/glew.o -c externLibs/GL/glew.c $(CXXFLAGS) -I$(includeLibs) -DGLEW_STATIC
	$(CXX) -shared -o $(bindir)$@ $^ obj/externLibs/glew.o $(LDFLAGS) $(CXXFLAGS) -I$(includeLibs) 
	$(CXX) -shared -o $(bindir)$@ $^ obj/externLibs/glew.o $(LDFLAGS) $(CXXFLAGS) -I$(includeLibs)
	
doc:
	doxygen doxyfile
	
clean-doc:
	rm -rf doc/*

depend: 
	$(CXX) -MM $(SRC) >depend

-include depend

$(objdir)%.o: %.cpp
	$(CXX) -o $@ -c $< $(CXXFLAGS) -I$(includedir) -I$(includeLibs)

.PHONY: install clean cleaninstall mrproper doc clean-doc

install:
	rm -rf /usr/lib/Glow
	mkdir /usr/lib/Glow
	cp lib/libGlow.so /usr/lib/Glow/
	rm -rf /usr/include/Glow
	cp -r src/ /usr/include/Glow
	rm -rf /usr/lib/libGlow.so*
	ln -s /usr/lib/Glow/libGlow.so /usr/lib/libGlow.so
	ldconfig

clean:
	rm -rf $(objdir)*.o
	rm -rf $(objdir)*/*.o

cleaninstall:
	rm -rf /usr/lib/Glow
	rm -rf /usr/include/Glow
	rm -rf /usr/lib/libGlow.so*

mrproper: clean clean-doc
	rm -rf $(bindir)$(EXE)
	rm -rf $(includedir)*/*~
	rm -rf $(srcdir)*~
	rm -rf *~
